#include "DXUT.h"
#include "DXUTgui.h"

#include "HUD.h"
#include "Item.h"

#ifdef RENDER

#define DXUT_FRONT_DEPTH 0.2f
#define DXUT_NEAR_BUTTON_DEPTH 0.6f
#define DXUT_FAR_BUTTON_DEPTH 0.8f


CHAR g_strUIBlockEffectFile[] = \
    "Texture2D g_Texture      : register(t0);"\
	"Texture2D g_TextureAlpha : register(t1);"\
    ""\
    "SamplerState Sampler"\
    "{"\
    "    Filter = MIN_MAG_MIP_POINT;"\
    "    AddressU = Wrap;"\
    "    AddressV = Wrap;"\
    "};"\
    ""\
    "BlendState UIBlend"\
    "{"\
    "    AlphaToCoverageEnable = FALSE;"\
    "    BlendEnable[0] = TRUE;"\
    "    SrcBlend = SRC_ALPHA;"\
    "    DestBlend = INV_SRC_ALPHA;"\
    "    BlendOp = ADD;"\
    "    SrcBlendAlpha = ONE;"\
    "    DestBlendAlpha = ZERO;"\
    "    BlendOpAlpha = ADD;"\
    "    RenderTargetWriteMask[0] = 0x0F;"\
    "};"\
    ""\
    "BlendState NoBlending"\
    "{"\
    "    BlendEnable[0] = FALSE;"\
    "    RenderTargetWriteMask[0] = 0x0F;"\
    "};"\
    ""\
    "DepthStencilState DisableDepth"\
    "{"\
    "    DepthEnable = false;"\
    "};"\
    "DepthStencilState EnableDepth"\
    "{"\
    "    DepthEnable = true;"\
    "};"\
    "struct VS_OUTPUT"\
    "{"\
    "    float4 Pos : POSITION;"\
    "    float4 Dif : COLOR;"\
    "    float2 Tex : TEXCOORD;"\
    "};"\
    ""\
    "VS_OUTPUT VS( float3 vPos : POSITION,"\
    "              float4 Dif : COLOR,"\
    "              float2 vTexCoord0 : TEXCOORD )"\
    "{"\
    "    VS_OUTPUT Output;"\
    ""\
    "    Output.Pos = float4( vPos, 1.0f );"\
    "    Output.Dif = Dif;"\
    "    Output.Tex = vTexCoord0;"\
    ""\
    "    return Output;"\
    "}"\
    ""\
    "float4 PS( VS_OUTPUT In ) : SV_Target"\
    "{"\
	"    float4 color = g_Texture.Sample( Sampler, In.Tex );"\
	"		if (color.a < 0.5) discard;"\
	"	 float4 colordif = g_TextureAlpha.Sample( Sampler, In.Tex );"\
	"		if (colordif.a > 0.5)"\
	"			color.rgb *= In.Dif.rgb;"\
	"	return color;"\
    "}"\
    ""\
    "float4 PSUntex( VS_OUTPUT In ) : SV_Target"\
    "{"\
    "    return In.Dif;"\
    "}"\
    ""\
    "technique10 RenderUI"\
    "{"\
    "    pass P0"\
    "    {"\
    "        SetVertexShader( CompileShader( vs_4_0, VS() ) );"\
    "        SetGeometryShader( NULL );"\
    "        SetPixelShader( CompileShader( ps_4_0, PS() ) );"\
    "        SetDepthStencilState( DisableDepth, 0 );"\
    "        SetBlendState( UIBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );"\
    "    }"\
    "}"\
    "technique10 RenderUIUntex"\
    "{"\
    "    pass P0"\
    "    {"\
    "        SetVertexShader( CompileShader( vs_4_0, VS() ) );"\
    "        SetGeometryShader( NULL );"\
    "        SetPixelShader( CompileShader( ps_4_0, PSUntex() ) );"\
    "        SetDepthStencilState( DisableDepth, 0 );"\
    "        SetBlendState( UIBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );"\
    "    }"\
    "}"\
    "technique10 RestoreState"\
    "{"\
    "    pass P0"\
    "    {"\
    "        SetDepthStencilState( EnableDepth, 0 );"\
    "        SetBlendState( NoBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );"\
    "    }"\
    "}";
const UINT              g_uUIBlockEffectFileSize = sizeof( g_strUIBlockEffectFile );

HUDCursor::HUDCursor( HUDDialog* pDialog ) {
	m_pDialog = pDialog;

	pDialog->SetTexture( 3, L"UI/test.png");

	CDXUTElement pElement;

	RECT prcTexture;
	prcTexture.bottom = 80;
	prcTexture.left = 16;
	prcTexture.right = 48;
	prcTexture.top = 48;

	pElement.SetTexture( 3, &prcTexture);
	pElement.SetFont( 0 );
	pElement.TextureColor.Current = D3DCOLOR_ARGB( 255, 255, 255, 255 );

	SetElement( 0, &pElement ); 
}

HUDCursor::~HUDCursor() {

}

void HUDCursor::Render( float fElapsedTime ) {
    if( m_bVisible == false )
        return;

	CDXUTElement* pElement = GetElement( 0 );

    RECT rcWindow = m_rcBoundingBox;
	DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL;

    pElement->TextureColor.Blend( iState, fElapsedTime );
    pElement->FontColor.Blend( iState, fElapsedTime );

    m_pDialog->DrawRelativeSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );
}

HUDLifebar::HUDLifebar( Player * player, HUDDialog* pDialog ) {
	m_pDialog = pDialog;
	m_pPlayer = player;
	m_blink				= 0.0f;
	m_lastHPValue		= 0.0f;
	m_lastHPregValue	= 0.0f;
	m_lastEPValue		= 0.0f;

	pDialog->SetTexture( 3, L"UI/test.png");

	CDXUTElement pElement;
	RECT prcTexture;
	prcTexture.bottom = 40;
	prcTexture.left = 10;
	prcTexture.right = 501;
	prcTexture.top = 16;
	pElement.SetTexture( 3, &prcTexture);
	pElement.SetFont( 0 );
	pElement.TextureColor.Current = D3DCOLOR_ARGB( 255, 255, 255, 255 );
	SetElement( 0, &pElement ); 

	CDXUTElement pHPGreenBar;
	prcTexture.bottom = 40;
	prcTexture.left = 6;
	prcTexture.right = 8;
	prcTexture.top = 16;
	pHPGreenBar.SetTexture( 3, &prcTexture);
	pHPGreenBar.SetFont( 0 );
	pHPGreenBar.TextureColor.Current = D3DCOLOR_ARGB( 255, 50, 150, 0 );
	SetElement( 1, &pHPGreenBar ); 

	CDXUTElement pHPRedBar;
	prcTexture.bottom = 40;
	prcTexture.left = 6;
	prcTexture.right = 8;
	prcTexture.top = 16;
	pHPRedBar.SetTexture( 3, &prcTexture);
	pHPRedBar.SetFont( 0 );
	pHPRedBar.TextureColor.Current = D3DCOLOR_ARGB( 255, 150, 0, 0 );
	SetElement( 2, &pHPRedBar ); 

	CDXUTElement pEPBorder;
	prcTexture.bottom = 40;
	prcTexture.left = 10;
	prcTexture.right = 501;
	prcTexture.top = 16;
	pEPBorder.SetTexture( 3, &prcTexture);
	pEPBorder.SetFont( 0 );
	pEPBorder.TextureColor.Current = D3DCOLOR_ARGB( 255, 255, 255, 255 );
	SetElement( 3, &pEPBorder ); 

	CDXUTElement pEP;
	prcTexture.bottom = 40;
	prcTexture.left = 6;
	prcTexture.right = 8;
	prcTexture.top = 16;
	pEP.SetTexture( 3, &prcTexture);
	pEP.SetFont( 0 );
	pEP.TextureColor.Current	= D3DCOLOR_ARGB( 255, 250, 200,  30 );
	pEP.TextureColor.States[0]	= D3DCOLOR_ARGB( 255, 250, 200,  30 );
	pEP.TextureColor.States[1]	= D3DCOLOR_ARGB( 255, 250,  50,  30 );
	pEP.TextureColor.States[2]	= D3DCOLOR_ARGB( 255, 255, 255, 255 );
	SetElement( 4, &pEP ); 

	CDXUTElement pFire;
	prcTexture.bottom = 512;
	prcTexture.left = 0;
	prcTexture.right = 56;
	prcTexture.top = 456;
	pFire.SetTexture( 3, &prcTexture);
	pFire.SetFont( 0 );
	pFire.TextureColor.Current = D3DCOLOR_ARGB( 255, 255, 255, 255 );
	SetElement( 5, &pFire ); 

	CDXUTElement pPoison;
	prcTexture.bottom = 456;
	prcTexture.left = 0;
	prcTexture.right = 56;
	prcTexture.top = 400;
	pPoison.SetTexture( 3, &prcTexture);
	pPoison.SetFont( 0 );
	pPoison.TextureColor.Current = D3DCOLOR_ARGB( 255, 255, 255, 255 );
	SetElement( 6, &pPoison ); 
}

HUDLifebar::~HUDLifebar() {

}

void HUDLifebar::Render( float fElapsedTime ) {
    if( m_bVisible == false )
        return;

	{
		CDXUTElement* pElement = GetElement( 2 );
		RECT rcWindow = m_rcBoundingBox;
		rcWindow.top	+= 1;
		rcWindow.bottom	-= 1;
		rcWindow.left	+= 4;
		rcWindow.right	-= 4;
		m_lastHPregValue = lerp(m_lastHPregValue, static_cast<float>(rcWindow.right - rcWindow.left) * (1.0f - m_pPlayer->getHPregRatio()), fElapsedTime * 20.0f);
		rcWindow.right  -= static_cast<int>(floor(m_lastHPregValue));
		m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );

		pElement = GetElement( 1 );
		rcWindow = m_rcBoundingBox;
		rcWindow.top	+= 1;
		rcWindow.bottom	-= 1;
		rcWindow.left	+= 4;
		rcWindow.right	-= 4;
		m_lastHPValue = lerp(m_lastHPValue, static_cast<float>(rcWindow.right - rcWindow.left) * (1.0f - m_pPlayer->getHPRatio()), fElapsedTime * 20.0f);
		rcWindow.right  -= static_cast<int>(floor(m_lastHPValue));
		m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );
	}

	{
		CDXUTElement* pElement = GetElement( 0 );
		RECT rcWindow = m_rcBoundingBox;  
		DXUT_CONTROL_STATE iState = DXUT_STATE_NORMAL;
		pElement->TextureColor.Blend( iState, fElapsedTime );
		pElement->FontColor.Blend( iState, fElapsedTime );
		m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );
	}


	{
		CDXUTElement* pElement = GetElement( 4 );
		RECT rcWindow = m_rcBoundingBox;
		rcWindow.top	+= 21;
		rcWindow.bottom	+= 19;	
		int width = static_cast<int>(floor(static_cast<float>(rcWindow.right - rcWindow.left) * m_pPlayer->getMaxEP() / 100.0f));
		rcWindow.right	= rcWindow.left + width - 4;
		rcWindow.left	+= 4;
		m_lastEPValue   = lerp(m_lastEPValue, static_cast<float>(rcWindow.right - rcWindow.left) * (1.0f - m_pPlayer->getEPRatio()), fElapsedTime * 20.0f);
		rcWindow.right  -= static_cast<int>(floor(m_lastEPValue));

		m_blink += fElapsedTime;
		if (m_blink > 0.3f) m_blink -= 0.6f;
		if (m_pPlayer->isJuced()) {
			if (m_blink >= 0.0f) {
				pElement->TextureColor.Blend( 0, fElapsedTime, 0.3f);
			} else {
				pElement->TextureColor.Blend( 2, fElapsedTime, 0.3f);
			}
		} else {
			if (m_pPlayer->isExhaused()) {
				if (m_blink >= 0.0f) {
					pElement->TextureColor.Blend( 0, fElapsedTime, 0.3f);
				} else {
					pElement->TextureColor.Blend( 1, fElapsedTime, 0.3f);
				}
			} else {
				pElement->TextureColor.Blend( 0, fElapsedTime, 0.3f);
			}
		}
		m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );

		pElement = GetElement( 3 );
		rcWindow = m_rcBoundingBox;  
		rcWindow.top += 20;
		rcWindow.bottom += 20;
		rcWindow.right	= rcWindow.left + width;
		m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );

		/* Status effects */			
		rcWindow = m_rcBoundingBox;  
		rcWindow.top += 40;
		rcWindow.bottom = rcWindow.top + 24;
		rcWindow.left += 10;
		rcWindow.right	= rcWindow.left + 24;

		if (m_pPlayer->isBurning()) {
			pElement = GetElement( 5 );
			m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );
		}

		if (m_pPlayer->isPoisoned()) {
			rcWindow.left  += 30;
			rcWindow.right += 30;
			pElement = GetElement( 6 );
			m_pDialog->DrawSprite11( pElement, &rcWindow, DXUT_FAR_BUTTON_DEPTH );
		}
	}
}

HUDSelected::HUDSelected( HUDDialog* pDialog ) {
	m_pDialog = pDialog;

	pDialog->SetTexture( 4, L"UI/Selected.png");

	CDXUTElement pElement;

	RECT prcTexture;
	prcTexture.bottom = 128;
	prcTexture.left = 0;
	prcTexture.right = 128;
	prcTexture.top = 0;

	pElement.SetTexture( 4, &prcTexture);
	pElement.SetFont( 0 );
	pElement.TextureColor.Current = D3DCOLOR_ARGB( 255, 255, 255, 255 );

	SetElement( 0, &pElement ); 
}

HUDSelected::~HUDSelected() {

}

void HUDSelected::Render( float fElapsedTime ) {
    if( m_bVisible == false )
        return;

	CDXUTElement* pElement = GetElement( 0 );

    RECT rcWindow = m_rcBoundingBox;

    m_pDialog->DrawRelativeSprite11( pElement, &rcWindow, 0.0f );
}

HUDItem::HUDItem( HUDInventoryDialog* pDialog ) {
	m_pDialog = pDialog;
	pDialog->SetTexture( 0, L"Textures/terrain.png");
	pDialog->SetTexture( 1, L"Textures/terrain_c.png");
	pDialog->SetTexture( 2, L"Textures/items.png");
}
/*
BYTE HUDItem::addBlock(int x, int y, Block block) {
	int id; memcpy(&id, &block, 4);
	ItemStruct vec = ItemStruct(x, id, y, rand() % 65);
	BYTE index = m_Blockpositions.size();
	m_Blockpositions.insert(std::pair<BYTE,ItemStruct>(index, vec));
	return index;
}

void HUDItem::setBlock(BYTE index, int x, int y, Block block) {
	int id; memcpy(&id, &block, 4);
	ItemStruct vec = ItemStruct(x, id, y, rand() % 65);
	auto it = m_Blockpositions.find(index);
	if (it != m_Blockpositions.end())
		it->second = vec;
	else
		m_Blockpositions.insert(std::pair<BYTE,ItemStruct>(index, vec));
};

BYTE HUDItem::addItem(int x, int y, DWORD id) {
	ItemStruct vec = ItemStruct(x, id, y, rand() % 65);
	BYTE index = m_Itempositions.size();
	m_Itempositions.insert(std::pair<BYTE,ItemStruct>(index, vec));
	return index;
}

void HUDItem::setItem(BYTE index, int x, int y, DWORD id) {
	ItemStruct vec = ItemStruct(x, id, y, rand() % 65);
	auto it = m_Itempositions.find(index);
	if (it != m_Itempositions.end())
		it->second = vec;
	else
		m_Itempositions.insert(std::pair<BYTE,ItemStruct>(index, vec));
};
*/
BYTE HUDItem::addItem(int x, int y, Item * item) {
	ItemStruct vec = ItemStruct(x, y, item);
	BYTE index = m_Itempositions.size();
	m_Itempositions.insert(std::pair<BYTE,ItemStruct>(index, vec));
	return index;
}

void HUDItem::removeItem(int x, int y) {
	ItemStruct vec = ItemStruct(x, y, NULL);
	for (auto it = m_Itempositions.begin(); it != m_Itempositions.end(); it++) {
		if (it->second == vec) {
			m_Itempositions.erase(it);
			break;
		}
	}
}

void HUDItem::setItem(BYTE index, int x, int y, Item * item) {
	ItemStruct vec = ItemStruct(x, y, item);
	auto it = m_Itempositions.find(index);
	if (it != m_Itempositions.end())
		it->second = vec;
	else
		m_Itempositions.insert(std::pair<BYTE,ItemStruct>(index, vec));
}

void HUDItem::Render( float fElapsedTime ) {
    if( m_bVisible == false )
        return;

    m_pDialog->DrawBlock11( &m_Itempositions, DXUT_FAR_BUTTON_DEPTH );
//	m_pDialog->DrawBlock11( &m_Blockpositions, DXUT_FAR_BUTTON_DEPTH );
};

HUDInventory::HUDInventory( Inventory * pInventory, HUDInventoryDialog* pDialog ) {
	m_pDialog = pDialog;

	pDialog->SetTexture( 0, L"Textures/terrain.png");
	pDialog->SetTexture( 1, L"Textures/terrain_c.png");
	pDialog->SetTexture( 2, L"Textures/items.png");

	m_pInventory = pInventory;
}

void HUDInventory::Render( float fElapsedTime ) {
    if( m_bVisible == false )
        return;

//	std::map<BYTE, ItemStruct>	Items;
	int width = m_pInventory->getLinewidth() * 50;
	m_width  = width;
	m_height = m_pInventory->getSize() / m_pInventory->getLinewidth() * 50;
	int x = 0;
	int y = 0;
	for(BYTE i = 0; i < m_pInventory->getSize(); i++) {
		Item * item = m_pInventory->getItemAtIndex(i);
		if (item) {
			//ItemStruct vec = ItemStruct(x + m_x, y + m_y, item);
			m_pDialog->DrawItem11( ItemStruct(x + m_x, y + m_y, item), DXUT_FAR_BUTTON_DEPTH );
			//Items.insert(pair<BYTE, ItemStruct>(i, vec));
		}
		x += 50;
		if (x > width) {
			x  =  0;
			y += 50;
		}
	}
  //  m_pDialog->DrawBlock11( &Items, DXUT_FAR_BUTTON_DEPTH );
}

void HUDDialog::Init( HUDDialogResourceManager* pManager, bool bRegisterDialog )
{
	m_pAdvManager = pManager;
    m_pManager = pManager;
    if( bRegisterDialog )
        pManager->RegisterDialog( this );

    SetTexture( 0, MAKEINTRESOURCE( 0xFFFF ), ( HMODULE )0xFFFF );
    InitDefaultElements();
}

HRESULT HUDDialog::AddCursor( int ID, bool bIsDefault, HUDCursor** ppCreated ) {
    HRESULT hr = S_OK;

    HUDCursor* pStatic = new HUDCursor( this );

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
	pStatic->SetLocation( 1024 / 2 - 16, 768 / 2 - 16 );
	pStatic->SetSize( 32, 32 );
    pStatic->m_bIsDefault = bIsDefault;

    return S_OK;
}

HRESULT HUDDialog::AddLifebar( Player * player, int ID, int x, int y, int width, int height, bool bIsDefault, HUDLifebar** ppCreated ) {
    HRESULT hr = S_OK;

    HUDLifebar* pStatic = new HUDLifebar( player, this );

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
    pStatic->SetLocation( x, y );
    pStatic->SetSize( width, height );
    pStatic->m_bIsDefault = bIsDefault;

    return S_OK;
}

HRESULT HUDDialog::AddSelected( int ID, int x, int y, int width, int height, bool bIsDefault, HUDSelected** ppCreated ) {
    HRESULT hr = S_OK;

    HUDSelected* pStatic = new HUDSelected( this );

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
    pStatic->SetLocation( x, y );
    pStatic->SetSize( width, height );
    pStatic->m_bIsDefault = bIsDefault;

    return S_OK;
}
/*
HRESULT HUDDialog::AddItem( int ID, HUDItem** ppCreated ) {
    HRESULT hr = S_OK;

    HUDItem* pStatic = new HUDItem( this );

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
    //pStatic->SetLocation( x, y );
   // pStatic->SetSize( width, height );
    pStatic->m_bIsDefault = false;

	return S_OK;
};

HRESULT HUDDialog::AddInventory( Inventory * pInventory, int ID, int x, int y, HUDInventory** ppCreated ) {
    HRESULT hr = S_OK;

    HUDInventory* pStatic = new HUDInventory( pInventory, this );

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
    pStatic->SetLocation( x, y );
   // pStatic->SetSize( width, height );
    pStatic->m_bIsDefault = false;

	return S_OK;
}

HRESULT HUDDialog::DrawItem11( ItemStruct item, float fDepth ) {
    float fBBWidth = ( float )m_pManager->m_nBackBufferWidth;
    float fBBHeight = ( float )m_pManager->m_nBackBufferHeight; 
   
    // Add 6 sprite vertices
    DXUTSpriteVertex	SpriteVertex;

	/* 
	    6
	   / \
	  /   \
	 /     \
    5       3
	 \     /
	| \   / |
	|  \ /  |
	|   0   |
	|   |   |
	4\  |  /2
	  \ | /
	   \ /
	    1
		
	item.x += m_x;
	item.y += m_y;
	BlockVertex * vertex = (BlockVertex*) malloc(sizeof(BlockVertex) * 48);
	WORD size = 0;
	if (item.item->Generate(vertex, &size) == -1)
		return S_OK;

	float fRectLeft = item.x / 1024.0f;//fBBWidth;
	float fRectTop = 1.0f - item.y / 768.0f;//fBBHeight;

	fRectLeft = fRectLeft * 2.0f - 1.0f;
	fRectTop = fRectTop * 2.0f - 1.0f;

	D3DXMATRIX ortho, mat1, mat2, mat3, mat4;
	D3DXMatrixRotationY(&mat1, D3DX_PI / 4);
	D3DXMatrixRotationX(&mat2, -D3DX_PI / 4);
	D3DXMatrixScaling(&mat3, 0.05f, fBBWidth / fBBHeight * 0.05f, 0.0f);
	D3DXMatrixTranslation(&mat4, fRectLeft, fRectTop, 1.0f);
	ortho = mat1 * mat2 * mat3 * mat4;

	for (byte x = 0; x < size;  x += 4) {
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[0 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[0 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[0 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[1 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[2 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );

		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[2 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[1 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[3 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[3 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[3 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
	}
	free(vertex);

	// No Geometry to be drawn
	if (m_pManager->m_SpriteVertices.GetSize() == 0)
		return S_OK;

	reinterpret_cast<HUDDialogResourceManager*>(m_pManager)->EndBlocks11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), GetTexture(0));

	BeginText11();

	item.x += m_x;
	item.y += m_y;
	DWORD count = item.item->getQuantity();
	DWORD cc = count;
	BYTE chars = 0;
	while (cc > 9) {
		cc    /= 10;
		chars += 1;
	}
	WCHAR str[10];
	swprintf_s( str, L"%d", count);
	item.x -= chars * 5;

	RECT rc;
	float sizeY = m_pManager->m_nBackBufferHeight / 768.0f;
	float sizeX = m_pManager->m_nBackBufferWidth  / 1024.0f;
	rc.top = (item.y		+ 11) * m_pManager->m_nBackBufferHeight/768;
	rc.left = (item.x	+ 11) * m_pManager->m_nBackBufferWidth/1024;
	rc.bottom = (item.y	+ 41) * m_pManager->m_nBackBufferHeight/768;
	rc.right = (item.x	+ 41) * m_pManager->m_nBackBufferWidth/1024;
	DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(0.1,0.1,0.1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );
	rc.top = (item.y		+ 10) * m_pManager->m_nBackBufferHeight/768;
	rc.left =  (item.x	+ 10) * m_pManager->m_nBackBufferWidth/1024;
	rc.bottom = (item.y	+ 40) * m_pManager->m_nBackBufferHeight/768;
	rc.right = (item.x	+ 40) * m_pManager->m_nBackBufferWidth/1024;
	DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(1,1,1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );

	return S_OK;
}

HRESULT HUDDialog::DrawItem11(  std::map<BYTE, ItemStruct>* items,  float fDepth, byte type ) {
    float fBBWidth = ( float )m_pManager->m_nBackBufferWidth;
    float fBBHeight = ( float )m_pManager->m_nBackBufferHeight; 
   
    // Add 6 sprite vertices
    DXUTSpriteVertex	SpriteVertex;

	/* 
	    6
	   / \
	  /   \
	 /     \
    5       3
	 \     /
	| \   / |
	|  \ /  |
	|   0   |
	|   |   |
	4\  |  /2
	  \ | /
	   \ /
	    1
		
	for (auto it = items->begin(); it != items->end(); it++) {
		ItemStruct vec = it->second;
		vec.x += m_x;
		vec.y += m_y;
		if (type == 0) {
			/*
			D3DXVECTOR2 * uv = getBlockUV(vec.z, FACELEFT);

			float fRectLeft = (vec.x - 20) / 1024.0f;
			float fRectTop = 1.0f - (vec.y - 20) / 768.0f;
			float fRectRight = (vec.x + 20) / 1024.0f;
			float fRectBottom = 1.0f - (vec.y + 20) / 768.0f;

			fRectLeft = fRectLeft * 2.0f - 1.0f;
			fRectTop = fRectTop * 2.0f - 1.0f;
			fRectRight = fRectRight * 2.0f - 1.0f;
			fRectBottom = fRectBottom * 2.0f - 1.0f;

			// Add 6 sprite vertices
			DXUTSpriteVertex SpriteVertex;

			// tri1
			SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectTop, fDepth );
			SpriteVertex.vTex = uv[2];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
			SpriteVertex.vTex = uv[3];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
			SpriteVertex.vTex = uv[0];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			// tri2
			SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
			SpriteVertex.vTex = uv[3];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectBottom, fDepth );
			SpriteVertex.vTex = uv[1];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
			SpriteVertex.vTex = uv[0];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );
			
		} else {
			BlockVertex * vertex = (BlockVertex*) malloc(sizeof(BlockVertex) * 48);
			WORD size = 0;
			if (vec.item->Generate(vertex, &size) == -1)
				return S_OK;

			float fRectLeft = vec.x / 1024.0f;//fBBWidth;
			float fRectTop = 1.0f - vec.y / 768.0f;//fBBHeight;

			fRectLeft = fRectLeft * 2.0f - 1.0f;
			fRectTop = fRectTop * 2.0f - 1.0f;

			D3DXMATRIX ortho, mat1, mat2, mat3, mat4;
			D3DXMatrixRotationY(&mat1, D3DX_PI / 4);
			D3DXMatrixRotationX(&mat2, -D3DX_PI / 4);
			D3DXMatrixScaling(&mat3, 0.05f, fBBWidth / fBBHeight * 0.05f, 0.0f);
			D3DXMatrixTranslation(&mat4, fRectLeft, fRectTop, 1.0f);
			ortho = mat1 * mat2 * mat3 * mat4;

			for (byte x = 0; x < size;  x += 4) {
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[0 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[0 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[0 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[1 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[2 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );

				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[2 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[1 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[3 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[3 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[3 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
			}
			free(vertex);
		}
	}
	// No Geometry to be drawn
	if (m_pManager->m_SpriteVertices.GetSize() == 0)
		return S_OK;

	if (type == 0) {
		m_pManager->EndSprites11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), GetTexture(2));
	} else {
		reinterpret_cast<HUDDialogResourceManager*>(m_pManager)->EndBlocks11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), GetTexture(0));
	}

	BeginText11();
	for (auto it = items->begin(); it != items->end(); it++) {
		ItemStruct vec = it->second;
		vec.x += m_x;
		vec.y += m_y;
		DWORD count = vec.item->getQuantity();
		DWORD cc = count;
		BYTE chars = 0;
		while (cc > 9) {
			cc    /= 10;
			chars += 1;
		}
		WCHAR str[10];
		swprintf_s( str, L"%d", count);
		vec.x -= chars * 5;

		RECT rc;
		float sizeY = m_pManager->m_nBackBufferHeight / 768.0f;
		float sizeX = m_pManager->m_nBackBufferWidth  / 1024.0f;
		rc.top = (vec.y		+ 11) * m_pManager->m_nBackBufferHeight/768;
		rc.left = (vec.x	+ 11) * m_pManager->m_nBackBufferWidth/1024;
		rc.bottom = (vec.y	+ 41) * m_pManager->m_nBackBufferHeight/768;
		rc.right = (vec.x	+ 41) * m_pManager->m_nBackBufferWidth/1024;
		DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(0.1,0.1,0.1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );
		rc.top = (vec.y		+ 10) * m_pManager->m_nBackBufferHeight/768;
		rc.left =  (vec.x	+ 10) * m_pManager->m_nBackBufferWidth/1024;
		rc.bottom = (vec.y	+ 40) * m_pManager->m_nBackBufferHeight/768;
		rc.right = (vec.x	+ 40) * m_pManager->m_nBackBufferWidth/1024;
		DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(1,1,1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );
	}

	return S_OK;
}

//--------------------------------------------------------------------------------------
HRESULT HUDDialog::DrawBlock11( std::map<BYTE, ItemStruct>* blocks, float fDepth )
{
	return DrawItem11( blocks, fDepth, 1 );
}
*/
HRESULT HUDDialog::DrawRelativeSprite11( CDXUTElement* pElement, RECT* prcDest, float fDepth )
{
    // No need to draw fully transparent layers
//    if( pElement->TextureColor.Current.a == 0 )
//        return S_OK;

    RECT rcTexture = pElement->rcTexture;

    RECT rcScreen = *prcDest;
    OffsetRect( &rcScreen, m_x, m_y );
	/*
	rcScreen.top = rcScreen.top * m_pManager->m_nBackBufferHeight/768;
	rcScreen.left = rcScreen.left * m_pManager->m_nBackBufferWidth/1024;
	rcScreen.bottom = rcScreen.bottom * m_pManager->m_nBackBufferHeight/768;
	rcScreen.right = rcScreen.right * m_pManager->m_nBackBufferWidth/1024;
	*/

    // If caption is enabled, offset the Y position by its height.
    if( m_bCaption )
        OffsetRect( &rcScreen, 0, m_nCaptionHeight );

    DXUTTextureNode* pTextureNode = GetTexture( pElement->iTexture );
    if( pTextureNode == NULL )
        return E_FAIL;
	/*
    float fBBWidth = ( float )m_pManager->m_nBackBufferWidth;
    float fBBHeight = ( float )m_pManager->m_nBackBufferHeight;
	*/
    float fTexWidth = ( float )pTextureNode->dwWidth;
    float fTexHeight = ( float )pTextureNode->dwHeight;

    float fRectLeft = rcScreen.left / 1024.0f;
    float fRectTop = 1.0f - rcScreen.top / 768.0f;
    float fRectRight = rcScreen.right / 1024.0f;
    float fRectBottom = 1.0f - rcScreen.bottom / 768.0f;

    fRectLeft = fRectLeft * 2.0f - 1.0f;
    fRectTop = fRectTop * 2.0f - 1.0f;
    fRectRight = fRectRight * 2.0f - 1.0f;
    fRectBottom = fRectBottom * 2.0f - 1.0f;
    
    float fTexLeft = rcTexture.left / fTexWidth;
    float fTexTop = rcTexture.top / fTexHeight;
    float fTexRight = rcTexture.right / fTexWidth;
    float fTexBottom = rcTexture.bottom / fTexHeight;

    // Add 6 sprite vertices
    DXUTSpriteVertex SpriteVertex;

    // tri1
    SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectTop, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexTop );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    // tri2
    SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectBottom, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexBottom );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    // Why are we drawing the sprite every time?  This is very inefficient, but the sprite workaround doesn't have support for sorting now, so we have to
    // draw a sprite every time to keep the order correct between sprites and text.
    m_pManager->EndSprites11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), pTextureNode);

    return S_OK;
}

HUDDialogResourceManager::HUDDialogResourceManager() {
	m_pVSRenderBlock11 = NULL;
    m_pPSRenderBlock11 = NULL;
	m_pSamplerStateBlock11 = NULL;
};

HRESULT HUDDialogResourceManager::OnD3D11CreateDevice( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext ) {
	HRESULT hr = CDXUTDialogResourceManager::OnD3D11CreateDevice(pd3dDevice, pd3d11DeviceContext);
	
	if (hr == S_OK) {
		// Compile Shaders
		ID3DBlob* pVSBlob = NULL;
		ID3DBlob* pPSBlob = NULL;
		
		V_RETURN( D3DCompile( g_strUIBlockEffectFile, g_uUIBlockEffectFileSize, "none", NULL, NULL, "VS", "vs_4_0_level_9_1", 
			D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pVSBlob, NULL ) );
		V_RETURN( D3DCompile( g_strUIBlockEffectFile, g_uUIBlockEffectFileSize, "none", NULL, NULL, "PS", "ps_4_0_level_9_1", 
			D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY, 0, &pPSBlob, NULL ) );
		
		// Create Shaders
		V_RETURN( pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &m_pVSRenderBlock11 ) );
		DXUT_SetDebugName( m_pVSRenderBlock11, "HUDDialogResourceManager" );

		V_RETURN( pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &m_pPSRenderBlock11 ) );
		DXUT_SetDebugName( m_pPSRenderBlock11, "HUDDialogResourceManager" );
		
		SAFE_RELEASE( pVSBlob );
		SAFE_RELEASE( pPSBlob );

		D3D11_SAMPLER_DESC SSDesc;
		ZeroMemory( &SSDesc, sizeof( D3D11_SAMPLER_DESC ) );
		SSDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
		SSDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
		SSDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
		SSDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
		SSDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
		SSDesc.MaxAnisotropy = 0;
		SSDesc.MinLOD = 0;
		SSDesc.MaxLOD = D3D11_FLOAT32_MAX;
		if ( pd3dDevice->GetFeatureLevel() < D3D_FEATURE_LEVEL_9_3 ) {
			SSDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
			SSDesc.MaxAnisotropy = 0;
		}
		V_RETURN( pd3dDevice->CreateSamplerState( &SSDesc, &m_pSamplerStateBlock11 ) );
		DXUT_SetDebugName( m_pSamplerStateBlock11, "HUDDialogResourceManagerr" );
	}
	return hr;
};

void HUDDialogResourceManager::OnD3D11DestroyDevice() {	
	SAFE_RELEASE( m_pVSRenderBlock11 );
    SAFE_RELEASE( m_pPSRenderBlock11 );
	SAFE_RELEASE( m_pSamplerStateBlock11 );
	CDXUTDialogResourceManager::OnD3D11DestroyDevice();
}

void HUDDialogResourceManager::EndBlocks11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, DXUTTextureNode* pTextureNode, DXUTTextureNode* pTextureNode2 ) {
    // ensure our buffer size can hold our sprites
    UINT SpriteDataBytes = m_SpriteVertices.GetSize() * sizeof( DXUTSpriteVertex );
    if( m_SpriteBufferBytes11 < SpriteDataBytes )
    {
        SAFE_RELEASE( m_pSpriteBuffer11 );
        m_SpriteBufferBytes11 = SpriteDataBytes;

        D3D11_BUFFER_DESC BufferDesc;
        BufferDesc.ByteWidth = m_SpriteBufferBytes11;
        BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
        BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
        BufferDesc.MiscFlags = 0;

        pd3dDevice->CreateBuffer( &BufferDesc, NULL, &m_pSpriteBuffer11 );
        DXUT_SetDebugName( m_pSpriteBuffer11, "HUDDialogResourceManager" );
    }

    D3D11_MAPPED_SUBRESOURCE MappedResource;
    if ( S_OK == pd3dImmediateContext->Map( m_pSpriteBuffer11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ) { 
        CopyMemory( MappedResource.pData, (void*)m_SpriteVertices.GetData(), SpriteDataBytes );
        pd3dImmediateContext->Unmap(m_pSpriteBuffer11, 0);
    }

    // Draw
    UINT Stride = sizeof( DXUTSpriteVertex );
    UINT Offset = 0;
	pd3dImmediateContext->PSSetShader( m_pPSRenderBlock11, NULL, 0 );

	if (pTextureNode)
		pd3dImmediateContext->PSSetShaderResources( 0, 1, &pTextureNode->pTexResView11 );
	if (pTextureNode2)
		pd3dImmediateContext->PSSetShaderResources( 1, 1, &pTextureNode2->pTexResView11 );

	//pd3dImmediateContext->PSSetShaderResources( 0, 1, &pTextureNode->pTexResView11 );
	pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateBlock11 );
    pd3dImmediateContext->IASetVertexBuffers( 0, 1, &m_pSpriteBuffer11, &Stride, &Offset );
    pd3dImmediateContext->IASetInputLayout( m_pInputLayout11 );
    pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
    pd3dImmediateContext->Draw( m_SpriteVertices.GetSize(), 0 );

	pd3dImmediateContext->PSSetShader( m_pPSRenderUI11, NULL, 0 );
	pd3dImmediateContext->PSSetSamplers( 0, 1, &m_pSamplerStateUI11 );
    m_SpriteVertices.Reset();
}



void HUDInventoryDialog::Init( HUDDialogResourceManager* pManager, bool bRegisterDialog )
{
	m_pAdvManager	= pManager;
    m_pManager		= pManager;
	m_pItemHold		= NULL;

    if( bRegisterDialog )
        pManager->RegisterDialog( this );

    SetTexture( 0, MAKEINTRESOURCE( 0xFFFF ), ( HMODULE )0xFFFF );
    InitDefaultElements();

	m_bLeftButtonDown	= false;
	m_bRightButtonDown  = false;
	m_bMiddleButtonDown = false;
	m_bSideButton1Down	= false;
	m_bSideButton2Down	= false;
}


HRESULT HUDInventoryDialog::AddItem( int ID, HUDItem** ppCreated ) {
    HRESULT hr = S_OK;

    HUDItem* pStatic = new HUDItem( this );

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
    //pStatic->SetLocation( x, y );
   // pStatic->SetSize( width, height );
    pStatic->m_bIsDefault = false;

	return S_OK;
};

HRESULT HUDInventoryDialog::AddInventory( Inventory * pInventory, int ID, int x, int y, HUDInventory** ppCreated ) {
    HRESULT hr = S_OK;

    HUDInventory* pStatic = new HUDInventory( pInventory, this );
	m_pInventuries.push_back(pStatic);

    if( ppCreated != NULL )
        *ppCreated = pStatic;

    if( pStatic == NULL )
        return E_OUTOFMEMORY;

    hr = AddElement( pStatic );
    if( FAILED( hr ) )
        return hr;

    // Set the ID and list index
    pStatic->SetID( ID );
    pStatic->SetLocation( x, y );
   // pStatic->SetSize( width, height );
    pStatic->m_bIsDefault = false;

	return S_OK;
}

HRESULT HUDInventoryDialog::DrawItem11( ItemStruct item, float fDepth ) {
    float fBBWidth = ( float )m_pManager->m_nBackBufferWidth;
    float fBBHeight = ( float )m_pManager->m_nBackBufferHeight; 
   
    // Add 6 sprite vertices
    DXUTSpriteVertex	SpriteVertex;

	/* 
	    6
	   / \
	  /   \
	 /     \
    5       3
	 \     /
	| \   / |
	|  \ /  |
	|   0   |
	|   |   |
	4\  |  /2
	  \ | /
	   \ /
	    1
		*/
	item.x += m_x;
	item.y += m_y;
	BlockVertex * vertex = (BlockVertex*) malloc(sizeof(BlockVertex) * 48);
	WORD size = 0;
	if ((item.item == NULL) || (item.item->Generate(vertex, &size) == -1)) {
		free(vertex);
		return S_OK;
	}

	float fRectLeft = item.x / 1024.0f;//fBBWidth;
	float fRectTop = 1.0f - item.y / 768.0f;//fBBHeight;

	fRectLeft = fRectLeft * 2.0f - 1.0f;
	fRectTop = fRectTop * 2.0f - 1.0f;

	D3DXMATRIX ortho, mat1, mat2, mat3, mat4;
	D3DXMatrixRotationY(&mat1, D3DX_PI / 4);
	D3DXMatrixRotationX(&mat2, -D3DX_PI / 4);
	D3DXMatrixScaling(&mat3, 0.05f, fBBWidth / fBBHeight * 0.05f, 0.0f);
	D3DXMatrixTranslation(&mat4, fRectLeft, fRectTop, 1.0f);
	ortho = mat1 * mat2 * mat3 * mat4;

	for (byte x = 0; x < size;  x += 4) {
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[0 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[0 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[0 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[1 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[2 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );

		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[2 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[1 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
		D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[3 + x].Pos, &ortho);
		SpriteVertex.vTex = vertex[3 + x].Tex;
		SpriteVertex.vColor = D3DXCOLOR(vertex[3 + x].Color);
		m_pManager->m_SpriteVertices.Add( SpriteVertex );
	}
	free(vertex);

	// No Geometry to be drawn
	if (m_pManager->m_SpriteVertices.GetSize() == 0)
		return S_OK;

	reinterpret_cast<HUDDialogResourceManager*>(m_pManager)->EndBlocks11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), GetTexture(0));

	BeginText11();

	item.x += m_x;
	item.y += m_y;
	DWORD count = item.item->getQuantity();
	DWORD cc = count;
	BYTE chars = 0;
	while (cc > 9) {
		cc    /= 10;
		chars += 1;
	}
	WCHAR str[10];
	swprintf_s( str, L"%d", count);
	item.x -= chars * 5;

	RECT rc;
	float sizeY = m_pManager->m_nBackBufferHeight / 768.0f;
	float sizeX = m_pManager->m_nBackBufferWidth  / 1024.0f;
	rc.top = (item.y		+ 11) * m_pManager->m_nBackBufferHeight/768;
	rc.left = (item.x	+ 11) * m_pManager->m_nBackBufferWidth/1024;
	rc.bottom = (item.y	+ 41) * m_pManager->m_nBackBufferHeight/768;
	rc.right = (item.x	+ 41) * m_pManager->m_nBackBufferWidth/1024;
	DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(0.1,0.1,0.1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );
	rc.top = (item.y		+ 10) * m_pManager->m_nBackBufferHeight/768;
	rc.left =  (item.x	+ 10) * m_pManager->m_nBackBufferWidth/1024;
	rc.bottom = (item.y	+ 40) * m_pManager->m_nBackBufferHeight/768;
	rc.right = (item.x	+ 40) * m_pManager->m_nBackBufferWidth/1024;
	DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(1,1,1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );

	return S_OK;
}

HRESULT HUDInventoryDialog::DrawItem11(  std::map<BYTE, ItemStruct>* items,  float fDepth, byte type ) {
    float fBBWidth = ( float )m_pManager->m_nBackBufferWidth;
    float fBBHeight = ( float )m_pManager->m_nBackBufferHeight; 
   
    // Add 6 sprite vertices
    DXUTSpriteVertex	SpriteVertex;

	/* 
	    6
	   / \
	  /   \
	 /     \
    5       3
	 \     /
	| \   / |
	|  \ /  |
	|   0   |
	|   |   |
	4\  |  /2
	  \ | /
	   \ /
	    1
		*/
	for (auto it = items->begin(); it != items->end(); it++) {
		ItemStruct vec = it->second;
		vec.x += m_x;
		vec.y += m_y;
		if (type == 0) {
			/*
			D3DXVECTOR2 * uv = getBlockUV(vec.z, FACELEFT);

			float fRectLeft = (vec.x - 20) / 1024.0f;
			float fRectTop = 1.0f - (vec.y - 20) / 768.0f;
			float fRectRight = (vec.x + 20) / 1024.0f;
			float fRectBottom = 1.0f - (vec.y + 20) / 768.0f;

			fRectLeft = fRectLeft * 2.0f - 1.0f;
			fRectTop = fRectTop * 2.0f - 1.0f;
			fRectRight = fRectRight * 2.0f - 1.0f;
			fRectBottom = fRectBottom * 2.0f - 1.0f;

			// Add 6 sprite vertices
			DXUTSpriteVertex SpriteVertex;

			// tri1
			SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectTop, fDepth );
			SpriteVertex.vTex = uv[2];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
			SpriteVertex.vTex = uv[3];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
			SpriteVertex.vTex = uv[0];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			// tri2
			SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
			SpriteVertex.vTex = uv[3];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectBottom, fDepth );
			SpriteVertex.vTex = uv[1];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );

			SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
			SpriteVertex.vTex = uv[0];
			SpriteVertex.vColor = D3DXCOLOR( 1.0, 1.0, 1.0, 1.0);
			m_pManager->m_SpriteVertices.Add( SpriteVertex );
			*/
		} else {
			BlockVertex * vertex = (BlockVertex*) malloc(sizeof(BlockVertex) * 48);
			WORD size = 0;
			if (vec.item->Generate(vertex, &size) == -1)
				return S_OK;

			float fRectLeft = vec.x / 1024.0f;//fBBWidth;
			float fRectTop = 1.0f - vec.y / 768.0f;//fBBHeight;

			fRectLeft = fRectLeft * 2.0f - 1.0f;
			fRectTop = fRectTop * 2.0f - 1.0f;

			D3DXMATRIX ortho, mat1, mat2, mat3, mat4;
			D3DXMatrixRotationY(&mat1, D3DX_PI / 4);
			D3DXMatrixRotationX(&mat2, -D3DX_PI / 4);
			D3DXMatrixScaling(&mat3, 0.05f, fBBWidth / fBBHeight * 0.05f, 0.0f);
			D3DXMatrixTranslation(&mat4, fRectLeft, fRectTop, 1.0f);
			ortho = mat1 * mat2 * mat3 * mat4;

			for (byte x = 0; x < size;  x += 4) {
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[0 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[0 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[0 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[1 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[2 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );

				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[2 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[2 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[2 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[1 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[1 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[1 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
				D3DXVec3TransformCoord(&SpriteVertex.vPos, &vertex[3 + x].Pos, &ortho);
				SpriteVertex.vTex = vertex[3 + x].Tex;
				SpriteVertex.vColor = D3DXCOLOR(vertex[3 + x].Color);
				m_pManager->m_SpriteVertices.Add( SpriteVertex );
			}
			free(vertex);
		}
	}
	// No Geometry to be drawn
	if (m_pManager->m_SpriteVertices.GetSize() == 0)
		return S_OK;

	if (type == 0) {
		m_pManager->EndSprites11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), GetTexture(2));
	} else {
		reinterpret_cast<HUDDialogResourceManager*>(m_pManager)->EndBlocks11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), GetTexture(0));
	}

	BeginText11();
	for (auto it = items->begin(); it != items->end(); it++) {
		ItemStruct vec = it->second;
		vec.x += m_x;
		vec.y += m_y;
		DWORD count = vec.item->getQuantity();
		DWORD cc = count;
		BYTE chars = 0;
		while (cc > 9) {
			cc    /= 10;
			chars += 1;
		}
		WCHAR str[10];
		swprintf_s( str, L"%d", count);
		vec.x -= chars * 5;

		RECT rc;
		float sizeY = m_pManager->m_nBackBufferHeight / 768.0f;
		float sizeX = m_pManager->m_nBackBufferWidth  / 1024.0f;
		rc.top = (vec.y		+ 11) * m_pManager->m_nBackBufferHeight/768;
		rc.left = (vec.x	+ 11) * m_pManager->m_nBackBufferWidth/1024;
		rc.bottom = (vec.y	+ 41) * m_pManager->m_nBackBufferHeight/768;
		rc.right = (vec.x	+ 41) * m_pManager->m_nBackBufferWidth/1024;
		DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(0.1,0.1,0.1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );
		rc.top = (vec.y		+ 10) * m_pManager->m_nBackBufferHeight/768;
		rc.left =  (vec.x	+ 10) * m_pManager->m_nBackBufferWidth/1024;
		rc.bottom = (vec.y	+ 40) * m_pManager->m_nBackBufferHeight/768;
		rc.right = (vec.x	+ 40) * m_pManager->m_nBackBufferWidth/1024;
		DrawText11DXUT( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), str, rc, D3DXCOLOR(1,1,1,1), (float)m_pManager->m_nBackBufferWidth, (float)m_pManager->m_nBackBufferHeight, false, sizeX, sizeY );
	}

	return S_OK;
}

//--------------------------------------------------------------------------------------
HRESULT HUDInventoryDialog::DrawBlock11( std::map<BYTE, ItemStruct>* blocks, float fDepth )
{
	return DrawItem11( blocks, fDepth, 1 );
}

HRESULT HUDInventoryDialog::DrawRelativeSprite11( CDXUTElement* pElement, RECT* prcDest, float fDepth )
{
    // No need to draw fully transparent layers
//    if( pElement->TextureColor.Current.a == 0 )
//        return S_OK;

    RECT rcTexture = pElement->rcTexture;

    RECT rcScreen = *prcDest;
    OffsetRect( &rcScreen, m_x, m_y );
	/*
	rcScreen.top = rcScreen.top * m_pManager->m_nBackBufferHeight/768;
	rcScreen.left = rcScreen.left * m_pManager->m_nBackBufferWidth/1024;
	rcScreen.bottom = rcScreen.bottom * m_pManager->m_nBackBufferHeight/768;
	rcScreen.right = rcScreen.right * m_pManager->m_nBackBufferWidth/1024;
	*/

    // If caption is enabled, offset the Y position by its height.
    if( m_bCaption )
        OffsetRect( &rcScreen, 0, m_nCaptionHeight );

    DXUTTextureNode* pTextureNode = GetTexture( pElement->iTexture );
    if( pTextureNode == NULL )
        return E_FAIL;
	/*
    float fBBWidth = ( float )m_pManager->m_nBackBufferWidth;
    float fBBHeight = ( float )m_pManager->m_nBackBufferHeight;
	*/
    float fTexWidth = ( float )pTextureNode->dwWidth;
    float fTexHeight = ( float )pTextureNode->dwHeight;

    float fRectLeft = rcScreen.left / 1024.0f;
    float fRectTop = 1.0f - rcScreen.top / 768.0f;
    float fRectRight = rcScreen.right / 1024.0f;
    float fRectBottom = 1.0f - rcScreen.bottom / 768.0f;

    fRectLeft = fRectLeft * 2.0f - 1.0f;
    fRectTop = fRectTop * 2.0f - 1.0f;
    fRectRight = fRectRight * 2.0f - 1.0f;
    fRectBottom = fRectBottom * 2.0f - 1.0f;
    
    float fTexLeft = rcTexture.left / fTexWidth;
    float fTexTop = rcTexture.top / fTexHeight;
    float fTexRight = rcTexture.right / fTexWidth;
    float fTexBottom = rcTexture.bottom / fTexHeight;

    // Add 6 sprite vertices
    DXUTSpriteVertex SpriteVertex;

    // tri1
    SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectTop, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexTop );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    // tri2
    SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectTop, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexTop );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectRight, fRectBottom, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexRight, fTexBottom );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    SpriteVertex.vPos = D3DXVECTOR3( fRectLeft, fRectBottom, fDepth );
    SpriteVertex.vTex = D3DXVECTOR2( fTexLeft, fTexBottom );
    SpriteVertex.vColor = pElement->TextureColor.Current;
    m_pManager->m_SpriteVertices.Add( SpriteVertex );

    // Why are we drawing the sprite every time?  This is very inefficient, but the sprite workaround doesn't have support for sorting now, so we have to
    // draw a sprite every time to keep the order correct between sprites and text.
    m_pManager->EndSprites11( DXUTGetD3D11Device(), DXUTGetD3D11DeviceContext(), pTextureNode);

    return S_OK;
}

void HUDInventoryDialog::handleMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos) {
	yPos *= 768;
	xPos *= 1024;
	yPos /= m_pManager->m_nBackBufferHeight;
	xPos /= m_pManager->m_nBackBufferWidth;
	xPos -= m_x;
	yPos -= m_y;

	if (bLeftButtonDown && !m_bLeftButtonDown) {
		int status = 0;
		Item * item = NULL;
		for (auto it = m_pInventuries.begin(); (it != m_pInventuries.end()) && (status == 0); it++ ) {
			HUDInventory * inv = (*it);
			status = inv->DragItem(xPos, yPos, &item);
		}
		if (status != 0) {
			if (item)
				m_pItemHold = item;
		}
	} else if (!bLeftButtonDown && m_bLeftButtonDown) {
		int status = 0;
		for (auto it = m_pInventuries.begin(); (it != m_pInventuries.end()) && (status == 0); it++ ) {
			HUDInventory * inv = (*it);
			if (m_pItemHold)
				status = inv->DropItem(xPos, yPos, &m_pItemHold);
		}
		if (status != 0) {
			m_pItemHold = NULL;
		}
	}

	m_bLeftButtonDown = bLeftButtonDown;
	m_bRightButtonDown = bRightButtonDown;
	m_bMiddleButtonDown = bMiddleButtonDown;
	m_bSideButton1Down = bSideButton1Down;
	m_bSideButton2Down = bSideButton2Down;
}
/*
void HUDInventory::handleMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos) {
	xPos -= m_x;
	yPos -= m_y;
	if (bLeftButtonDown) {
		BYTE index = (xPos / 50) + ((yPos / 50) * m_pInventory->getLinewidth());
		m_pInventory->removeItem(index);
	}
}
*/
int HUDInventory::DragItem( int xPos, int yPos, Item ** item) {
	xPos -= m_x - 25;
	yPos -= m_y - 25;
	if ((xPos > 0) && (yPos > 0) && (xPos < m_width) && (yPos < m_height)) {
		BYTE width = m_pInventory->getLinewidth();
		BYTE index = ((xPos / 50) % width) + ((yPos / 50) * width);
		*item =	m_pInventory->getItemAtIndex(index);
				m_pInventory->NullItem(index);
		return 1;
	} else
		return 0;
}

int HUDInventory::DropItem( int xPos, int yPos, Item ** item) {
	xPos -= m_x - 25;
	yPos -= m_y - 25;
	if ((xPos > 0) && (yPos > 0) && (xPos < m_width) && (yPos < m_height)) {
		BYTE width = m_pInventory->getLinewidth();
		BYTE index = ((xPos / 50) % width) + ((yPos / 50) * width);
		//item =	m_pInventory->getItemAtIndex(index);
				m_pInventory->removeItem(index);
				m_pInventory->setItem(*item, index);
		return 1;
	} else
		return 0;
}

#endif